﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ext.Net;
using Ext.Net.MVC;
using com.subb.WebFramework;
using WebEntities;
using WebEntities.Models.Framework;

namespace WebAdmin.Controllers
{
    [Authorize]
    public class MenuItemController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetMenuItemsByPage(StoreRequestParameters parms)
        {
            FW_MenuItemDAL mdal = new FW_MenuItemDAL();
            int ttlCount;
            List<UIMenuItem> itemlist = mdal.GetAllUIMenuItems(parms.Start, parms.Limit, out ttlCount);
            return this.Store(itemlist, ttlCount);
        }

        public ActionResult GetAllMenuItems(int? itemId)
        {
            FW_MenuItemDAL mdal = new FW_MenuItemDAL();
            List<FW_MenuItem> dbitemlist = mdal.GetAllMenuItems();
            if (itemId.HasValue)
            {
                FW_MenuItem muitem = dbitemlist.SingleOrDefault(c => c.ItemID == itemId);
                if (muitem != null)
                {
                    dbitemlist.Remove(muitem);
                }
            }
            List<UIMenuItem> itemlist = new List<UIMenuItem>();
            foreach (var item in dbitemlist)
            {
                //这里不需要显示MenuName和ParentItemName，所以赋值为空
                UIMenuItem muitem = UIMenuItem.AsUIMenuItem(item, "", "");
                itemlist.Add(muitem);
            }
            return this.Store(itemlist);
        }

        public ActionResult GetAllMenuItemsByMenuId(int menuId)
        {
            FW_MenuItemDAL mdal = new FW_MenuItemDAL();
            List<FW_MenuItem> itemList = mdal.GetMenuItemsByMenuID(menuId);
            return this.Store(itemList);
        }

        [HttpPost]
        public ActionResult DeleteMenuItem(List<UIMenuItem> delmuItem)
        {
            try
            {
                FW_MenuItemDAL idal = new FW_MenuItemDAL();
                foreach (var mitem in delmuItem)
                {
                    idal.RemoveMenuItem(mitem.ItemID);
                }
                return this.Direct(new { Success = true, Msg = "删除成功！" });
            }
            catch (Exception ex)
            {
                return this.Direct(new { Success = false, Msg = ex.InnerException != null ? ex.InnerException.Message : ex.Message });
            }
        }

        public ActionResult NewMenuItemWindow()
        {
            Window wnd = new Window()
            {
                ID = "Window1",
                Title = "新建菜单项",
                Width = 700,
                Height = 400,
                Modal = true,
                Maximizable = true,
                Icon = Ext.Net.Icon.LayoutAdd,
                Loader = new ComponentLoader
                {
                    Url = Url.Action("CreateMenuItem"),
                    Mode = LoadMode.Frame,
                    LoadMask =
                    {
                        ShowMask = true,
                        Msg = "加载中，请稍候..."
                    }
                }
            };
            wnd.Render();
            return this.Direct();
        }

        public ActionResult CreateMenuItem()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CreateMenuItem(UIMenuItem muItem)
        {
            FormPanelResult response = new FormPanelResult();
            try
            {
                FW_MenuItemDAL mdal = new FW_MenuItemDAL();
                string creator = Request.IsAuthenticated ? HttpContext.User.Identity.Name : "Anonymous";
                mdal.CreateMenuItem(muItem.ItemName, muItem.MenuID, muItem.ItemUrl, muItem.MvcArea, muItem.MvcCtrl, muItem.MvcAction, muItem.ParentID, creator, muItem.SortNum);
                response.Success = true;
                response.ExtraParams.Add(new Parameter("Msg", "菜单项创建成功！"));
                return response;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.ExtraParams.Add(new Parameter("Msg", ex.InnerException != null ? ex.InnerException.Message : ex.Message));
                return response;
            }
        }

        public ActionResult GetAllMenu()
        {
            FW_MenuDAL mdal = new FW_MenuDAL();
            List<FW_Menu> dbmulist = mdal.GetAllMenus();
            List<UIMenu> mulist = new List<UIMenu>();
            foreach (var menu in dbmulist)
            {
                UIMenu uimenu = UIMenu.AsUIMenu(menu);
                mulist.Add(uimenu);
            }
            return this.Store(mulist);
        }

        public ActionResult EditMenuItemWindow(int itemId)
        {
            Window wnd = new Window()
            {
                ID = "Window1",
                Title = "编辑菜单项",
                Width = 700,
                Height = 400,
                Modal = true,
                Maximizable = true,
                Icon = Ext.Net.Icon.LayoutAdd,
                Loader = new ComponentLoader
                {
                    Url = Url.Action("EditMenuItem"),
                    Mode = LoadMode.Frame,
                    LoadMask =
                    {
                        ShowMask = true,
                        Msg = "加载中，请稍候..."
                    }
                }
            };
            wnd.Loader.Params.Add(new Parameter("itemId", itemId.ToString()));
            wnd.Render();
            return this.Direct();
        }

        public ActionResult EditMenuItem(int itemId)
        {
            FW_MenuItemDAL idal = new FW_MenuItemDAL();
            FW_MenuItem muitem = idal.GetMenuItem(itemId);
            //这里不需要显示MenuName和ParentItemName，所以赋值为空
            UIMenuItem uitem = UIMenuItem.AsUIMenuItem(muitem, "", "");
            return View(uitem);
        }

        [HttpPost]
        public ActionResult EditMenuItem(UIMenuItem muitem)
        {
            FormPanelResult response = new FormPanelResult();
            try
            {
                FW_MenuItemDAL idal = new FW_MenuItemDAL();
                idal.UpdateMenuItem(muitem.ItemID, muitem);
                response.Success = true;
                response.ExtraParams.Add(new Parameter("Msg", "菜单更新成功！"));
                return response;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.ExtraParams.Add(new Parameter("Msg", ex.InnerException != null ? ex.InnerException.Message : ex.Message));
                return response;
            }
        }
    }
}
